package com.yunquan.ohana.utils

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.res.Configuration
import android.text.format.DateFormat
import androidx.core.content.ContextCompat
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import com.yunquan.ohana.base.ui.AppManager
import java.time.Instant
import java.time.LocalDate
import java.time.LocalTime
import com.yunquan.ohana.R
import java.util.Locale

object DateTimeHelper {

    /**
     * 按指定语言显示原生 DatePickerDialog（无标题栏/隐藏标题），并设置按钮颜色。
     * @param localeTag IETF 语言标签，例如 "zh-CN"、"en-US"。
     */
    fun showDatePicker(
        selectedDate: LocalDate?,
        onDatePicked: (LocalDate) -> Unit,
    ) {
        val activity = AppManager.appManager.currentActivity()!!
        val baseDate = selectedDate ?: LocalDate.now()

        val year = baseDate.year
        val month = baseDate.monthValue - 1
        val day = baseDate.dayOfMonth

        val dialog = DatePickerDialog(
            LocaleHelper.wrap(activity, Locale.getDefault()),
            R.style.ThemeDateDialog,
            { _, y, m, d ->
                onDatePicked(LocalDate.of(y, m + 1, d))
            },
            year, month, day
        )

        // 隐藏标题（移除对话框标题）
        dialog.setTitle("")

        dialog.setOnShowListener {
            val color = ContextCompat.getColor(activity, R.color.c_FF9932)
            dialog.getButton(DatePickerDialog.BUTTON_POSITIVE)?.apply {
                setTextColor(color)
                try {
                    typeface = activity.resources.getFont(R.font.montserrat_semibold)
                } catch (_: Throwable) {
                }
            }
            dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE)?.apply {
                setTextColor(color)
                try {
                    typeface = activity.resources.getFont(R.font.montserrat_semibold)
                } catch (_: Throwable) {
                }
            }
        }

        dialog.show()
    }

    /**
     * 按指定语言显示原生 TimePickerDialog（无标题栏/隐藏标题），并设置按钮颜色。
     * @param localeTag IETF 语言标签，例如 "zh-CN"、"en-US"。
     * @param is24Hour 是否使用 24 小时制
     */
    fun showTimePicker(
        selectedTime: LocalTime?,
        is24Hour: Boolean = DateFormat.is24HourFormat(AppManager.appManager.currentActivity()),
        onTimePicked: (LocalTime) -> Unit,
    ) {
        val activity = AppManager.appManager.currentActivity()!!
        val baseTime = selectedTime ?: LocalTime.now()

        val dialog = TimePickerDialog(
            LocaleHelper.wrap(activity, Locale.getDefault()),
            R.style.ThemeTimeDialog,
            { _, hourOfDay, minute ->
                onTimePicked(LocalTime.of(hourOfDay, minute))
            },
            baseTime.hour,
            baseTime.minute,
            is24Hour
        )

        // 隐藏标题（移除对话框标题）
        dialog.setTitle("")

        dialog.setOnShowListener {
            val color = ContextCompat.getColor(activity, R.color.c_FF9932)
            dialog.getButton(TimePickerDialog.BUTTON_POSITIVE)?.apply {
                setTextColor(color)
                try {
                    typeface = activity.resources.getFont(R.font.montserrat_semibold)
                } catch (_: Throwable) {
                }
            }
            dialog.getButton(TimePickerDialog.BUTTON_NEGATIVE)?.apply {
                setTextColor(color)
                try {
                    typeface = activity.resources.getFont(R.font.montserrat_semibold)
                } catch (_: Throwable) {
                }
            }
        }

        dialog.show()
    }

    /**
     * 使用指定 Locale 包装 Context（仅影响该 Context 创建的视图/对话框）。
     */
    private fun wrapContextWithLocale(base: Context, locale: Locale): Context {
        val config = Configuration(base.resources.configuration)
        config.setLocale(locale)
        return base.createConfigurationContext(config)
    }
}